home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: news.sprintlink.net!news1!news
- From: rclark@iquest.net (Robert B. Clark)
- Subject: Re: Function wanted to evaluate string
- X-Nntp-Posting-Host: ind-004-236-169.iquest.net
- Message-ID: <3129dcde.870616@news.iquest.net>
- Sender: news@iquest.net (News Admin)
- Organization: IQuest Internet, Inc.
- X-Newsreader: Forte Agent .99d/16.182
- References: <4g20eo$623@coranto.ucs.mun.ca>
- Date: Tue, 20 Feb 1996 15:45:08 GMT
-
- On 16 Feb 1996 13:22:32 GMT, cdeacon@kelvin.physics.mun.ca (Chris
- Deacon) wrote:
-
- >I want a function which will evaluate a string of numbers and
- >arithmetic operators. That is, whwn I call
- >
- >eval_func(2*5)
- >
- > a value of 10 will be returned.
-
- Somewhat facetiously,
-
- int eval_func(int i); return i;
-
- will perform the task to the letter of your specification. :-)
-
- I assume that what you *meant* is that you want a routine to parse a
- mathematical expression submitted as a string;
-
- int eval_func(const char *expr) /* Ex, expr="2*5-3" */
-
- which is a bit more involved and reminds me of an "infinite precision"
- calculator program that I had to write back in CS52.
-
- Briefly (it's been a while),
-
- 1. Tokenize the string. Isolate left and right terms and operators
- inner- to outermost. Think recursion.
- 2. Evaluate/reduce terms
- 3. Apply operators.
- 4. Go back to 1.
-
- I'm sure that there are some good references on the subject; perhaps
- some of the other participants can provide some additional pointers.
- --
- Robert B. Clark <rclark@iquest.net>
- "Be wary of strong spirits. It can make you shoot at tax collectors...
- and miss." --RAH
-